package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.navigation.fragment.NavHostFragment;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import cn.kailangtu.commonevents.eventEntity.FragmentChangedEvent;
import cn.kailangtu.commonevents.eventEntity.OnFragmentChangeEvent;
import cn.kailangtu.commonevents.eventEntity.ViewShowSwitchEvent;
import cn.kailangtu.commontools.AppVariable;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.data.ControlTree;
import cn.kailangtu.dataspot.data.PageIndex;
import cn.kailangtu.dataspot.data.context.SelectContext;
import cn.kailangtu.dataspot.data.consts.ControlAction;
import cn.kailangtu.dataspot.databinding.FragmentSwitchTestLowBinding;
import cn.kailangtu.dataspot.status;
import cn.kailangtu.dataspot.tasks.BaseTask;

public class SwitchTestLowFragment extends BaseFragment {

    private FragmentSwitchTestLowBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentSwitchTestLowBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        EventBus.getDefault().post(new FragmentChangedEvent(R.layout.fragment_switch_test_low));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ViewShowSwitchEvent event)
    {
        if(!event.isShowDuanlu())
        {
            binding.rlSwitchHeza.setVisibility(View.GONE);
        }

        if(!event.isShowGuozai()){
            binding.rlSwitchGuozai.setVisibility(View.GONE);
        }
        if(!event.isShowHeza()){
            binding.rlSwitchHeza.setVisibility(View.GONE);
        }
        if(!event.isShowKaiza()){
            binding.rlSwitchFenza.setVisibility(View.GONE);
        }
        if(!event.isShowLoudian()){
            binding.rlSwitchLoudian.setVisibility(View.GONE);
        }
        if(!event.isShowZijian()){
            binding.rlSwitchZijian.setVisibility(View.GONE);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OnFragmentChangeEvent event) {
        // Do something
        if(R.layout.fragment_factory_setting_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(SwitchTestLowFragment.this)
                    .navigate(R.id.action_SwitchTestLowFragment_to_FactorySettingLowFragment);
        }
        else if(R.layout.fragment_switch_test_low == event.getTargetFragmentId())
        {

        }
        else if(R.layout.fragment_common_setting_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(SwitchTestLowFragment.this)
                    .navigate(R.id.action_SwitchTestLowFragment_to_CommonSettingLowFragment);
        }
        else if(R.layout.fragment_exception_log_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(SwitchTestLowFragment.this)
                    .navigate(R.id.action_SwitchTestLowFragment_to_ExceptionLogLowFragment);
        }
        else if(R.layout.fragment_real_picture_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(SwitchTestLowFragment.this)
                    .navigate(R.id.action_SwitchTestLowFragment_to_RealPictureLowFragment);
        }
    }


    @Override
    protected int processControlsEvent(int eventType) {
        if(eventType == ControlAction.Selection)
        {
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            SelectContext.setSelectedView(textView);
        }
        else if(eventType == ControlAction.Entry){
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            textView.callOnClick();
        }
        return 0;
    }

    @Override
    protected void buildControlTree() {
        controlTree = ControlTree.getLowSwitch();
    }


    @Override
    protected void resetPageIndex() {

        if (status.localToPlc != 2) {
            status.localPageIndex = PageIndex.L_SWITCH;
            BaseTask.toNewPage(status.localPageIndex);
        }
        else
        {
            if(status.plcPageIndex == PageIndex.L_SWITCH){
                status.localPageIndex = PageIndex.L_SWITCH;
            }
            else if(status.plcPageIndex == PageIndex.L_STOP)
            {
               // binding.tvSwitchTestTitle7.callOnClick();
            }
        }
    }

    @Override
    void resetPlatform() {
        if(AppVariable.platform_version == 450 && AppVariable.need_reset_view) {
            commonReset(binding.rlSwitchTestContainer);
        }
    }
}